import tkinter as tk
import docx
import os

def create_word_document(company, name, job, address, phone):
    doc = docx.Document()
    # 设置页面边距
    sections = doc.sections
    for section in sections:
        section.top_margin = docx.shared.Inches(1)
        section.bottom_margin = docx.shared.Inches(1)
        section.left_margin = docx.shared.Inches(1)
        section.right_margin = docx.shared.Inches(1)
    # 添加照片（假设照片路径为'D:\\photo.jpg'，可根据实际情况修改）
    if os.path.exists('D:\\photo.jpg'):
        doc.add_picture('D:\\photo.jpg',
                        width=docx.shared.Inches(2),
                        height=docx.shared.Inches(2))
    # 添加文本内容
    doc.add_paragraph('******************************')
    doc.add_paragraph(company)
    doc.add_paragraph(f'{name}\t{job}')
    doc.add_paragraph('------------------------------')
    doc.add_paragraph(address)
    doc.add_paragraph(phone)
    doc.add_paragraph('****************************')
    # 设置中文字体为宋体并尝试另一种方式设置字体大小
    for paragraph in doc.paragraphs:
        for run in paragraph.runs:
            run.font.name = '宋体'
            run.font.size = docx.shared.Mm(10)
    doc.save('D:\\个人信息.docx')

def tkwindows():
    top = tk.Tk()
    top.title("课程展示")
    top.geometry("600x300")
    # 单位
    unit_label = tk.Label(top, text="单位:")
    unit_label.pack()
    unit_entry = tk.Entry(top)
    unit_entry.pack()
    # 姓名
    name_label = tk.Label(top, text="姓名:")
    name_label.pack()
    name_entry = tk.Entry(top)
    name_entry.pack()
    # 职务
    job_label = tk.Label(top, text="职务:")
    job_label.pack()
    job_entry = tk.Entry(top)
    job_entry.pack()
    # 联系方式
    contact_label = tk.Label(top, text="联系方式:")
    contact_label.pack()
    contact_entry = tk.Entry(top)
    contact_entry.pack()
    # 联系地址
    address_label = tk.Label(top, text="联系地址:")
    address_label.pack()
    address_entry = tk.Entry(top)
    address_entry.pack()

    def generate_word():
        unit = unit_entry.get()
        name = name_entry.get()
        job = job_entry.get()
        contact = contact_entry.get()
        address = address_entry.get()
        create_word_document(unit, name, job, address, contact)

    # 生成Word文档按钮
    generate_button = tk.Button(top,
                                text="生成Word文档",
                                command=generate_word)
    generate_button.pack()
    top.mainloop()

if __name__ == "__main__":
    tkwindows()